<template>
  <div>
    <Icon
      v-if="!electric.sites[props.renderRow.electricName]"
      :name="state.Icon"
      style="cursor: pointer"
      @click="getSiteName"
    />
    <el-tag v-else>
      {{ electric.sites[props.renderRow.electricName] }}
    </el-tag>
  </div>
</template>
<script lang="ts" setup>
import _ from "lodash";
import { onMounted, reactive, inject } from "vue";
import type { TableColumn, TableRow } from "@/utils/baTable"; //表格列行类型
import { ElNotification } from "element-plus"; //处理导航信息

const cabinet = inject("cabinet") as any;
const site = inject("site") as any;
const electric = inject("electric") as any;

// BuildAdmin将自动为此组件传递五个Props
interface Props {
  renderValue: any; // 单元格值
  renderRow: TableRow; // 当前行数据
  renderField: TableColumn; // 当前列数据
}

const props = withDefaults(defineProps<Props>(), {});

const state: { Icon: any; type: string; siteNameOfArea: string } = reactive({
  Icon: "el-icon-Pointer",
  type: "",
  siteNameOfArea: "",
});

//获取字典值
const getDistrictNum = (val: string) => {
  let len = site.type.length;
  for (let i = 0; i < len; ) {
    if (site.type[i].dictLabel === val) {
      return site.type[i].dictValue - 0;
    } else {
      i++;
    }
  }
};

//查询电表管理获取电表的所属站点
const getSiteName = () => {
  site
    .getFilterList({
      siteName: props.renderRow.electricName,
      siteType: getDistrictNum("电表"),
    })
    .then((res: any) => {
      if (res.length > 0) {
        //获取电表类型的站点获取祖籍id数组 length-3为地区类型站点
        let idArr = res[0].ancestors.split(",");
        let len = idArr.length;
        site
          .getfilterById({
            siteId: idArr[len - 3],
          })
          .then((res: any) => {
            if (
              JSON.stringify(electric.sites) == "{}" ||
              !JSON.stringify(electric.sites[props.renderRow.electricName])
            ) {
              electric.sites[props.renderRow.electricName] = res.siteName;
            }
          });
      } else {
        ElNotification({
          type: "warning",
          offset: 100,
          // dangerouslyUseHTMLString: true,
          message: "站点查询为空 请先绑定网关",
        });
      }

      console.log(
        "props.renderRow.siteName",
        props.renderRow.siteName,
        cabinet.sites
      );
    });
};

onMounted(() => {
  // console.log("当前行数据", props.renderRow, "当前列数据", props.renderField);
  // console.log(
  //   "props.renderRow.siteName",
  //   props.renderRow.siteName,
  //   cabinet.sites
  // );
});
</script>
<style lang="scss" scoped></style>
